package org.jeecg.modules.wlhy.tj;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.ArrayListDict;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerAddressCommon;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerAddressCommonService;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.tj.mapper.HyTjMapper;
import org.jeecg.modules.wlhy.tj.vo.HyCJTjVO;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@Api(tags="统计分析")
@RestController
@RequestMapping("/tj")
@Slf4j
public class HyTJController extends JeecgController<HyCustomerAddressCommon, IHyCustomerAddressCommonService> {

    @Autowired
    private HyTjMapper hyTjMapper;

    @Autowired
    private UserUtil userUtil;

    @Autowired
    private IHyVehicleService hyVehicleService;

    @GetMapping(value = "/order")
    @ArrayListDict
    public Result<?> orderTj(@RequestParam(required = false) Map<String,String> params) {
        HyCustomer customer = userUtil.getCustomer();
        if(customer!=null){
            params.put("customerId" , customer.getId());
        }
        List<?> list = hyTjMapper.getOrderTj(params);
        log.error("统计数据:"+list.toString());
        return Result.OK(list);
    }

    //差价运单统计
    @GetMapping(value = "/cjtj")
    @ArrayListDict
    public Result<?> cjTj(@RequestParam(required = false) Map<String,String> params) {
        HyCustomer customer = userUtil.getCustomer();
        if(customer!=null){
            params.put("customerId" , customer.getId());
        }
        List<?> list = hyTjMapper.getCjTj(params);
        return Result.OK(list);
    }

    //运费统计
    @GetMapping(value = "/yunfeitj")
    @ArrayListDict
    public Result<?> yunfeitj(@RequestParam(required = true) Map<String,String> params) {

        List<?> list = hyTjMapper.getYunfeiTj(params);
        return Result.OK(list);
    }

    //所有人统计
    @GetMapping(value = "/suoyourentj")
    @ArrayListDict
    public Result<?> suoyourentj(@RequestParam(required = true) Map<String,String> params) {

        List<HyCJTjVO> list = hyTjMapper.getsuoyourenTj(params);
        for (HyCJTjVO hyCJTjVO : list) {
            Integer count = hyVehicleService.count(new QueryWrapper<HyVehicle>().eq("owner", hyCJTjVO.getOwner()));
            hyCJTjVO.setOwnercount(count.toString()==null ?"0":count.toString());
        }

        return Result.OK(list);
    }
    //里程统计
    @GetMapping(value = "/lichengtj")
    @ArrayListDict
    public Result<?> lichengtj(@RequestParam(required = true) Map<String,String> params) {

        List<HyCJTjVO> list = hyTjMapper.lichengtj(params);

        return Result.OK(list);
    }

}
